package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.HsStringUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.p2p.bean.UpdateJxPhone;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by xx on 2017/7/10.
 */
public class UpdateJXReservedPhoneActivity extends BaseActivity implements InitializeView,View.OnClickListener{
    private TextView tv_phone;
    private HSEditText et_new_phone;
    private HSEditText et_vcode;
    private ValidateButton btn_vcode;
    private RequestPresenter mPresenter;
    private String zwgt_data;
    private HSButton btn_update_pay_pwd;
    private String newphone;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_update_jx_reserved_phone;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        //进行网络请求
        mPresenter = new InitializePresenterImpl(mContext,this);
        setToolbarTitle("修改银行预留手机号");

        tv_phone.setText(getString(R.string.reserve_phone_,HsStringUtils.abbreviate(sharedPreferences.getShareProf(UserInfo.BANKMOBILE), "*****", 3, 8)));

        btn_vcode.init(false, "RechangeQuickFragment");

        btn_vcode.setOnClickListener(this);

        btn_update_pay_pwd.setOnClickListener(this);

        et_new_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                String newPhoneNumTmp = s.toString().replaceAll(" ", "");
                // TODO Auto-generated method stub
                if (HsStringUtils.isBlank(newphone))
                    return;
                if (newPhoneNumTmp.length() != 11)
                    return;
                if (!newphone.equals(newPhoneNumTmp))
                    btn_vcode.reset();
            }
        });
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    private void findView(){
        tv_phone = findView(R.id.tv_phone);
        et_new_phone = findView(R.id.et_new_phone);
        et_vcode = findView(R.id.et_vcode);
        btn_vcode = findView(R.id.btn_vcode);
        btn_update_pay_pwd = findView(R.id.btn_update_pay_pwd);
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name){
            case TaskConstants.TASK_CHANGEJXPHONE://修改江西银行预留手机号
                showToast("修改成功");

                UpdateJxPhone updateJxPhone = (UpdateJxPhone) data;
                Logs.e("jiijijiji" + updateJxPhone.phone);
                finish();
                BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                //保存新的手机号
                sharedPreferences.putShareProf(UserInfo.BANKMOBILE,updateJxPhone.phone);
                EventBus.getDefault().post(new EventCenter(Constants.EVENT_REFRESH_DATA));//登陆成功通知某些页面进行数据刷新
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_vcode:
                newphone = et_new_phone.getPhone();

                if (RegularUtils.isPhoneNum(newphone)){
                    zwgt_data = BaseRequestTool2.getFormatedDateString();
                    JSONObject map = new JSONObject();

                    try {
                        map.put("srvtxcode", "mobileModifyPlus");
                        map.put("phone", newphone);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    btn_vcode.setCountdown();

                    JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                    mPresenter.loadingData(TaskConstants.TASK_SMS_APPLY, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, Constants.EVENT_DATA,
                            jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, sharedPreferences
                                    .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);

                } else {
                    showToast("请输入正确的手机号");
                }

                break;

            case R.id.btn_update_pay_pwd:
                //开始修改交易密码
                String vCode = et_vcode.getText().toString();
                newphone = et_new_phone.getPhone();

                //检查验证码是否为空
                if (TextUtils.isEmpty(vCode)){
                    showToast(R.string.empty_vcode);
                    return;
                }

                //检查验证码格式
                if (!RegularUtils.isLength(vCode,6)) {
                    showToast(R.string.error_vcode);
                    return;
                }

                if (RegularUtils.isPhoneNum(newphone)){
                    zwgt_data = BaseRequestTool2.getFormatedDateString();
                    JSONObject map1 = new JSONObject();

                    try {
                        map1.put("newphone", newphone);
                        map1.put("smscode", vCode);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    JSONObject jsonParams1 = CommTool.getJSONParams(map1, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                    mPresenter.loadingData(TaskConstants.TASK_CHANGEJXPHONE, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_CHANGEJXPHONE, Constants.EVENT_DATA,
                            jsonParams1, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_CHANGEJXPHONE, sharedPreferences
                                    .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
                } else {
                    showToast("请输入正确的手机号");
                }

                break;
        }
    }
}
